package com.tourlaid.mamagerment.ao.wx.impl

import com.tourlaid.mamagerment.ao.pc.CommonAo
import com.tourlaid.mamagerment.enums.StoreTypeEnum
import com.tourlaid.mamagerment.service.*
import com.tourlaid.mamagerment.vo.LinkageVo
import com.tourlaid.mamagerment.vo.SelectVo
import org.springframework.stereotype.Service
import org.springframework.util.CollectionUtils
import java.util.*
import javax.annotation.Resource

/**
 * Created by xiaoyongge on 2017/7/29.
 */
@Service("commonAo")
class CommonAoImpl : CommonAo {

    @Resource
    val goodsService: GoodsService? = null
    @Resource
    val supplierService: SupplierService? = null
    @Resource
    val shopService: ShopService? = null
    @Resource
    val stockInService: StockInService? = null
    @Resource
    val areaService: AreaService? = null

    override fun getGoodsSelectVo(): List<SelectVo> {
        val goodsList = goodsService!!.queryAllGoods()
        var voList = ArrayList<SelectVo>()
        for (goods in goodsList) {
            var vo = SelectVo()
            vo.id = goods.id
            vo.value = goods.goodsName
            voList.add(vo)
        }
        return voList
    }

    override fun getSupplierSelectVo(): List<SelectVo> {
        val supplierList = supplierService!!.queryAllSupplier()
        var voList = ArrayList<SelectVo>()
        for (supplier in supplierList) {
            var vo = SelectVo()
            vo.id = supplier.id
            vo.value = supplier.supplierName
            voList.add(vo)
        }
        return voList
    }

    override fun getShopSelectVo(): List<SelectVo> {
        val shopList = shopService!!.queryAllShop()
        var voList = ArrayList<SelectVo>()
        for (shop in shopList) {
            var vo = SelectVo()
            vo.id = shop.id
            vo.value = shop.shopName
            voList.add(vo)
        }
        return voList
    }

    override fun getStockInSelectVo(goodsId: Long): List<SelectVo> {
        val stockInList = stockInService!!.queryByGoodsId(goodsId)
        var voList = ArrayList<SelectVo>()
        for (stockIn in stockInList) {
            var vo = SelectVo()
            vo.id = stockIn.id
            vo.value = stockIn.controlNo
            voList.add(vo)
        }
        return voList
    }

    override fun getStoreTypeLinkageVo(): List<LinkageVo> {
        var voList = ArrayList<LinkageVo>()
        val allLink = LinkageVo("不限", "0", "")
        voList.add(allLink)
        for (storeType in StoreTypeEnum.values()) {
            val vo = LinkageVo(storeType.desc, storeType.code.toString(), "")
            voList.add(vo)
        }
        return voList
    }

    override fun getStoreTypeLinkageVo2(): List<LinkageVo> {
        var voList = ArrayList<LinkageVo>()
        for (storeType in StoreTypeEnum.values()) {
            var vo = LinkageVo(storeType.desc, storeType.code.toString(), "")
            voList.add(vo)
        }
        return voList
    }

    override fun getAreaList(cityName: String): MutableList<LinkageVo> {
        var list: MutableList<LinkageVo> = mutableListOf()
        val area = areaService!!.getByAreaName(cityName)
        if (area == null) return list
        val arealist = areaService!!.queryByParentId(area.id!!)
        if (CollectionUtils.isEmpty(arealist)) return list
        val allArea1 = LinkageVo("不限", "不限", "0")
        val allArea2 = LinkageVo("全部", "全部", "不限")
        list.add(allArea1)
        list.add(allArea2)
        val parentIdList = arealist!!.map { area2 -> area2.id!! }
        val distinctList = areaService!!.queryByParentIdList(parentIdList)
        for (tempArea in arealist) {
            val subDisList = distinctList.filter { area3 -> Objects.equals(area3.parentId, tempArea.id) }
            val area1 = LinkageVo(tempArea.areaName!!, tempArea.areaName!!, "0")
            list.add(area1)
            val distinct = LinkageVo("全部", "全部", tempArea.areaName!!)
            list.add(distinct)
            for (tempDistinct in subDisList) {
                val distinct1 = LinkageVo(tempDistinct.areaName!!, tempDistinct.areaName!!, tempArea.areaName!!)
                list.add(distinct1)
            }
        }
        return list
    }

    override fun getAreaLinkageVo(parentName: String): MutableList<LinkageVo> {
        var list: MutableList<LinkageVo> = mutableListOf()
        val area = areaService!!.getByAreaName(parentName)
        if (area == null) return list
        val arealist = areaService!!.queryByParentId(area.id!!)
        if (CollectionUtils.isEmpty(arealist)) return list
        for (tempArea in arealist!!) {
            val distinct = LinkageVo(tempArea.areaName!!, tempArea.areaName!!, "")
            list.add(distinct)
        }
        return list
    }
}